package com.mj.circle;

import com.mj.AbstractList;

/**
 * 单向循环链表
 *
 * @param <E>
 */
public class SingleCircleLinkedList<E> extends AbstractList<E> {

    private Node<E> first;
    //用于指向某个节点
    private Node<E> current;

    private static class Node<E> {
        E element;
        Node<E> next;

        public Node(E element, Node<E> next) {
            this.element = element;
            this.next = next;
        }

        @Override
        public String toString() {
            StringBuilder sb = new StringBuilder();
            //因为是循环的 所以下一个都有值
            sb.append(element).append("_").append(next.element);

            return sb.toString();
        }
    }

    /**
     * 让 current 指向头结点 first
     */
    public void reset() {
        current = first;
    }

    /**
     * 让 current 往后走一步，也就是 current = current.next
     *
     * @return
     */
    public E next() {

        if (current == null) return null;
        current = current.next;
        return current.element;

    }

    /**
     * 删除 current 指向的节点，删除成功后让 current 指向下一个节点
     *
     * @return
     */
    public E remove() {
        if (current == null) return null;
        Node<E> next = current.next;
        E element = remove(indexOf(current.element));// 这个方法太笨重
        if (size == 0) current = null;
        else current = next;
        return element;
    }


    @Override
    public void clear() {
        size = 0;
        first = null;
    }

    /**
     * 获取节点中的数据
     *
     * @param index
     * @return
     */
    @Override
    public E get(int index) {

        return getNode(index).element;
    }

    /**
     * 先把之前的元素取出来,在覆盖
     *
     * @param index
     * @param element
     * @return
     */
    @Override
    public E set(int index, E element) {
        Node<E> oldNode = getNode(index);
        E oldElement = oldNode.element;
        oldNode.element = element;
        return oldElement;
    }

    @Override
    public void add(int index, E element) {
        rangeCheckForAdd(index);
        if (index == 0) {
            Node<E> newFirst = new Node<>(element, first);
            //拿到最后一个位置的节点
            Node<E> last = (size == 0) ? newFirst : getNode(size - 1);
            last.next = newFirst;
            first = newFirst;

        } else {
            Node<E> pre = getNode(index - 1);

            pre.next = new Node<>(element, pre.next);
        }
        size++;
    }

    /**
     * 获取索引所在的节点
     *
     * @param index
     * @return
     */
    private Node<E> getNode(int index) {
        rangeCheck(index);
        Node<E> node = first;
        for (int i = 0; i < index; i++) {
            node = node.next;
        }
        return node;
    }

    @Override
    public E remove(int index) {
        rangeCheck(index);
        Node<E> node = first;
        if (index == 0) {
            if (size == 1) {
                first = null;
            } else {
                Node<E> last = getNode(size - 1);
                first = node.next;
                last.next = first;
            }
        } else {
            Node<E> pre = getNode(index - 1);
            node = pre.next;
            pre.next = node.next;
        }
        size--;
        return node.element;
    }

    @Override
    public int indexOf(E element) {
        Node<E> node = first;
        if (element == null) {
            for (int i = 0; i < size; i++) {
                if (node.element == null) return i;
                node = node.next;
            }
        } else {
            for (int i = 0; i < size; i++) {
                // 因为element肯定不为null了,所以放在前面;避免空指针异常
                if (node.element.equals(element)) return i;
                node = node.next;
            }
        }
        return ELEMENT_NOT_FOUND;
    }


    @Override
    public String toString() {
        StringBuilder string = new StringBuilder();
        Node<E> node = this.first;
        string.append("size=").append(size).append(", [");
        for (int i = 0; i < size; i++) {
            string.append(node);
            node = node.next;
            if (i != size - 1) {
                string.append(", ");
            }
        }
//        while (node !=null){
//            node = node.next;
//        }

        string.append("]");
        return string.toString();
    }
}
